%% On the Optimal Design of Transfers and Income-Tax Progressivity
% Computation of data targets from NIPA and Financial Accounts for 2012

clear; clc; close all; 

%% Load required data

% NIPA
% Tables downloaded from https://apps.bea.gov/iTable/?reqid=19&step=2&isuri=1&categories=survey 
% Download on November 6, 2022
% Last data revision: October 27, 2022
nipa_table_1_1_5 = readtable('Table_1_1_5.xlsx','Sheet','Table','Range','A6:CQ34','PreserveVariableNames',true);
nipa_table_2_1 = readtable('Table_2_1.xlsx','Sheet','Table','Range','A6:CQ53','PreserveVariableNames',true);
nipa_table_3_1 = readtable('Table_3_1.xlsx','Sheet','Table','Range','A6:CQ51','PreserveVariableNames',true);
nipa_table_3_2 = readtable('Table_3_2.xlsx','Sheet','Table','Range','A6:CQ57','PreserveVariableNames',true);
nipa_table_3_3 = readtable('Table_3_3.xlsx','Sheet','Table','Range','A6:CQ52','PreserveVariableNames',true);
nipa_table_3_12 = readtable('Table_3_12.xlsx','Sheet','Table','Range','A6:CQ50','PreserveVariableNames',true);

% Financial Accounts of the United States (flow of funds)
% Table Financial Accounts Z1 : D3, debt outstanding by sector
% Table downloaded from https://www.federalreserve.gov/datadownload/Choose.aspx?rel=z1
% Download on November 7, 2022
% Statistical release for September 9, 2022
fa_z1_d3 = readtable('FRB_Z1.xls','Sheet','Sheet1','Range','A2:CE12','PreserveVariableNames',true);

%% Consumption tax

% Federal taxes
tau_c_fed_excise = table2array(nipa_table_3_2(5,86));        % taxes on production and imports: excise taxes (NIPA Table 3.2; line 5)
tau_c_fed_customs = table2array(nipa_table_3_2(6,86));       % taxes on production and imports: customs duties (NIPA Table 3.2; line 6)
tau_c_fed_other = table2array(nipa_table_3_2(7,86));         % taxes on production and imports: other (NIPA Table 3.2; line 7)

% State and local taxes
tau_c_state_sales = table2array(nipa_table_3_3(7,86));       % taxes on production and imports: sales taxes (NIPA Table 3.3; line 7)
tau_c_state_excise = table2array(nipa_table_3_3(8,86));      % taxes on production and imports: excise taxes (NIPA Table 3.3; line 8)
tau_c_state_other = table2array(nipa_table_3_3(10,86));      % taxes on production and imports: other (NIPA Table 3.3; line 10)

% Personal consumption expenditure
personal_consumption = table2array(nipa_table_2_1(29,86));   % personal consumption expenditures (NIPA Table 2.1; line 29)

% Estimated consumption tax
tau_c = (tau_c_fed_excise+tau_c_fed_customs+tau_c_fed_other+tau_c_state_sales+tau_c_state_excise+tau_c_state_other)/personal_consumption;

disp(['Estimate for the consumption tax is = ', num2str(tau_c*100), '%'])
disp(' ')

%% Capital tax

% Taxes
tau_k_corp = table2array(nipa_table_3_1(5,86));              % taxes on corporate income (NIPA Table 3.1; line 5)
inc_div = table2array(nipa_table_2_1(15,86));                % personal dividend income (NIPA Table 2.1; line 15)
tau_div = 0.133;                                             % estimate of Bhandari/McGrattan
tau_k_div = inc_div*tau_div;                                 % taxes on dividends

% GDP
gdp = table2array(nipa_table_1_1_5(1,86));                    % NIPA Table 1.1.5; line 1

% Capital taxes relative to GDP
tau_k_gdp = (tau_k_corp+tau_k_div)/gdp;

% Translate into model terms
r = 0.02;                       % interest rate
alpha = 0.64;                   % labor share
delta = 0.06;                   % depreciation rate

% Estimated capital tax
tau_k = (tau_k_gdp*(r+delta))/(r*(1-alpha));

disp(['Estimate for the capital tax is = ', num2str(tau_k*100), '%'])
disp(' ')

%% Government spending and receipts 

% Receipts
receipts_tot = table2array(nipa_table_3_1(1,86));           % current receipts (NIPA table 3.1; line 1)
receipts_assets = table2array(nipa_table_3_1(10,86));       % income receipts on assets (NIPA table 3.1; line 10)
receipts_transfers = table2array(nipa_table_3_1(15,86));    % current transfer receipts (NIPA table 3.1; line 15)
receipts_surplus = table2array(nipa_table_3_1(19,86));      % current surplus of government enterprises (NIPA table 3.1; line 19)
receipts_2 = receipts_tot - receipts_assets - receipts_transfers - receipts_surplus; % receipts from taxes and contributions for government social insurance

% Total expenditure
exp_tot = table2array(nipa_table_3_1(20,86));               % current expenditures (NIPA table 3.1; line 20)

% Interest expenditures
exp_irates = table2array(nipa_table_3_1(27,86));            % interest payments (NIPA table 3.1; line 27)

% Transfer expenditures
%ui = table2array(nipa_table_3_12(7,86));                      % unemployment insurance (NIPA table 3.12; line 7)
snap = table2array(nipa_table_3_12(21,86));                    % food stamps (NIPA table 3.12; line 21)
credits = table2array(nipa_table_3_12(25,86));                 % refundable tax credits (NIPA table 3.12; line 25)
fam_assist = table2array(nipa_table_3_12(35,86));              % family assistance (NIPA table 3.12; line 35)
gen_assist = table2array(nipa_table_3_12(37,86));              % general assistance (NIPA table 3.12; line 37)
ene_assist = table2array(nipa_table_3_12(38,86));              % energy assistance (NIPA table 3.12; line 38)
oth_assist = table2array(nipa_table_3_12(39,86));              % other assistance (NIPA table 3.12; line 39)

transfers = snap + credits + fam_assist + gen_assist + ene_assist + oth_assist;

disp(['Estimate for total receipts relative to GDP is = ',num2str(100*receipts_2/gdp),'%'])
disp(['Estimate for total expenditures relative to GDP is = ',num2str(100*exp_tot/gdp),'%'])
disp(['Estimate for transfer expenditures relative to GDP is = ',num2str(100*transfers/gdp),'%'])
disp(['Estimate for interest expenditures relative to GDP is = ',num2str(100*exp_irates/gdp),'%'])
disp(['Estimate for remaining expenditures relative to GDP is = ',num2str((100*exp_tot-100*transfers-100*exp_irates)/gdp),'%'])
disp(' ')

%% Flow of Funds

federal_debt = table2array(fa_z1_d3(7,74));
state_debt = table2array(fa_z1_d3(8,74));
public_debt = (federal_debt + state_debt)/(gdp*1000);

disp(['Estimate for public debt relative to GDP is = ',num2str(100*public_debt),'%'])






